# coding: utf-8

import Background
import ControlPoint
from PSOFT.vendor.Qt_py.Qt import QtCore, QtWidgets


class PencilCurveScene(QtWidgets.QGraphicsScene):

    xyTextUpdate = QtCore.Signal(float, float)
    symButtonUpdate = QtCore.Signal(bool)

    def __init__(self, parent=None):
        super(PencilCurveScene, self).__init__(parent)

        def defaultCallback(cp):
            pass
        self.cpCreatedCallback = defaultCallback
        self.cpChangedCallback = defaultCallback
        self.cpDestroyedCallback = defaultCallback
        self.xyTextUpdateCallback = defaultCallback

        self.background = Background.Background()
        self.addItem(self.background)

        self.controlPoints = ControlPoint.ControlPoint()
        self.addItem(self.controlPoints)

    # Returns a list of the Maya indices for the selected control points
    def getSelectedMayaIndices(self):
        return [cp.mayaIndex for cp in self.controlPoints.model.selection()]

    # Selects the control points matching the specified Maya indices
    def setSelectedMayaIndices(self, idx):
        for cp in self.controlPoints.model:
            if cp.mayaIndex in idx:
                cp.selected = True

    def allControlPointsDeselect(self):
        for cp in self.controlPoints.model:
            cp.selected = False

    def redrawCurve(self):
        self.controlPoints.model.sort()
        self.controlPoints.update()

    def applyHandleType(self, handleType):
        self.controlPoints.setHandleType(handleType)
        self.controlPoints.update()
        self.background.update()  # MEMO: 必ず呼ぶ。呼ばないと線の描画が残ったままになる

    def reverseCurve(self):
        self.controlPoints.reverse()
        self.controlPoints.update()
        self.background.update()  # MEMO: 必ず呼ぶ。呼ばないと線の描画が残ったままになる

    def symmetrizeCurve(self):
        self.controlPoints.symmetrize()
        self.controlPoints.update()
        self.background.update()  # MEMO: 必ず呼ぶ。呼ばないと線の描画が残ったままになる

    def setMayaCallback(self, cpCreated=None, cpChanged=None, cpDestroyed=None):
        self.cpCreatedCallback = cpCreated
        self.cpChangedCallback = cpChanged
        self.cpDestroyedCallback = cpDestroyed
